#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk
import sys

f=open('/dev/led0','w')

def write(string):
	try:
		f.seek(0,0)
		f.write(string)
		f.flush()
	except Exception, e:
		print e
		sys.exit(1)

def sendcommand(widget, command):
	if widget.get_active():
		write(command)

def lightness(widget, command):
	write('%s %d'%(command,int(widget.value)) )

def ontime(widget, color):
	write('%sontime %d'%(color,int(widget.value)) )

def offtime(widget, color):
	write('%sofftime %d'%(color,int(widget.value)) )
	
def flashfreq(widget):
	write('flashfreq %d'%(int(widget.value)))

def loudspeaker(widget, a1, a2):
	write('loudspeaker %d %d'%(int(a1.value), int(a2.value)))

def destroy(widget, data=None):
	write('blueoff')
	write('redoff')
	write('greenoff')
	gtk.main_quit()

window = gtk.Window()
window.set_border_width(10)
window.set_title('USB LED')
window.connect('destroy', destroy)

# 3 state buttons
on = gtk.RadioButton(None, 'ON')
on.connect('toggled', sendcommand, 'blueon')
flash = gtk.RadioButton(on, 'FLASH')
flash.connect('toggled', sendcommand, 'blueflash')
off = gtk.RadioButton(on, 'OFF')
off.connect('toggled', sendcommand, 'blueoff')
off.set_active(True)
# 'lightness' HScale
a=gtk.Adjustment(255, 0, 255, 10, 10)
a.connect('value_changed', lightness, 'bluelightness')
sc = gtk.HScale(a)
sc.set_digits(0)
sc.set_value_pos(gtk.POS_LEFT)
# pack label & HScale
b = gtk.HBox(False, 10)
b.pack_start(gtk.Label('Lightness'), False, False)
b.pack_start(sc)
# 'ontime' HScale
a=gtk.Adjustment(255, 0, 255, 10, 10)
a.connect('value_changed', ontime, 'blue')
sc = gtk.HScale(a)
sc.set_digits(0)
sc.set_value_pos(gtk.POS_LEFT)
b2 = gtk.HBox(False, 10)
b2.pack_start(gtk.Label('OnTime'), False, False)
b2.pack_start(sc)
# 'offtime' HScale
a=gtk.Adjustment(255, 0, 255, 10, 10)
a.connect('value_changed', offtime, 'blue')
sc = gtk.HScale(a)
sc.set_digits(0)
sc.set_value_pos(gtk.POS_LEFT)
b3 = gtk.HBox(False, 10)
b3.pack_start(gtk.Label('OffTime'), False, False)
b3.pack_start(sc)
# put them into one table
t = gtk.Table(4, 3, True)
t.attach(on, 0, 1, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(flash, 1, 2, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(off, 2, 3, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(b, 0, 3, 1, 2, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(b2, 0, 3, 2, 3, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(b3, 0, 3, 3, 4, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
# put table into frame
fb = gtk.Frame('Blue')
fb.add(t)

# 3 state buttons
on = gtk.RadioButton(None, 'ON')
on.connect('toggled', sendcommand, 'redon')
flash = gtk.RadioButton(on, 'FLASH')
flash.connect('toggled', sendcommand, 'redflash')
off = gtk.RadioButton(on, 'OFF')
off.connect('toggled', sendcommand, 'redoff')
off.set_active(True)
# 'lightness' HScale
a=gtk.Adjustment(255, 0, 255, 10, 10)
a.connect('value_changed', lightness, 'redlightness')
sc = gtk.HScale(a)
sc.set_digits(0)
sc.set_value_pos(gtk.POS_LEFT)
# pack lightness Label & HScale
b = gtk.HBox(False, 10)
b.pack_start(gtk.Label('Lightness'), False, False)
b.pack_start(sc)
# 'OnTime' HScale
a=gtk.Adjustment(255, 0, 255, 10, 10)
a.connect('value_changed', ontime, 'red')
sc = gtk.HScale(a)
sc.set_digits(0)
sc.set_value_pos(gtk.POS_LEFT)
b2 = gtk.HBox(False, 10)
b2.pack_start(gtk.Label('OnTime'), False, False)
b2.pack_start(sc)
# 'OffTime' HScale
a=gtk.Adjustment(255, 0, 255, 10, 10)
a.connect('value_changed', offtime, 'red')
sc = gtk.HScale(a)
sc.set_digits(0)
sc.set_value_pos(gtk.POS_LEFT)
b3 = gtk.HBox(False, 10)
b3.pack_start(gtk.Label('OffTime'), False, False)
b3.pack_start(sc)
# put them into one table
t = gtk.Table(4, 3, True)
t.attach(on, 0, 1, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(flash, 1, 2, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(off, 2, 3, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(b, 0, 3, 1, 2, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(b2, 0, 3, 2, 3, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(b3, 0, 3, 3, 4, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
# pack table into frame
fr = gtk.Frame('Red')
fr.add(t)

# 3 state buttons
on = gtk.RadioButton(None, 'ON')
on.connect('toggled', sendcommand, 'greenon')
flash = gtk.RadioButton(on, 'FLASH')
flash.connect('toggled', sendcommand, 'greenflash')
off = gtk.RadioButton(on, 'OFF')
off.connect('toggled', sendcommand, 'greenoff')
off.set_active(True)
# 'lightness' HScale
a=gtk.Adjustment(255, 0, 255, 10, 10)
a.connect('value_changed', lightness, 'greenlightness')
sc = gtk.HScale(a)
sc.set_digits(0)
sc.set_value_pos(gtk.POS_LEFT)
b = gtk.HBox(False, 10)
b.pack_start(gtk.Label('Lightness'), False, False)
b.pack_start(sc)
# 'OnTime' HScale
a=gtk.Adjustment(255, 0, 255, 10, 10)
a.connect('value_changed', ontime, 'green')
sc = gtk.HScale(a)
sc.set_digits(0)
sc.set_value_pos(gtk.POS_LEFT)
b2 = gtk.HBox(False, 10)
b2.pack_start(gtk.Label('OnTime'), False, False)
b2.pack_start(sc)
# 'OffTime' HScale
a=gtk.Adjustment(255, 0, 255, 10, 10)
a.connect('value_changed', offtime, 'green')
sc = gtk.HScale(a)
sc.set_digits(0)
sc.set_value_pos(gtk.POS_LEFT)
b3 = gtk.HBox(False, 10)
b3.pack_start(gtk.Label('OffTime'), False, False)
b3.pack_start(sc)
# put them into one table
t = gtk.Table(4, 3, True)
t.attach(on, 0, 1, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(flash, 1, 2, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(off, 2, 3, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(b, 0, 3, 1, 2, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(b2, 0, 3, 2, 3, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
t.attach(b3, 0, 3, 3, 4, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 5, 5)
fg = gtk.Frame('Green')
fg.add(t)

box=gtk.VBox(False, 10)
# pack three frames
box.pack_start(fb, False, False, 0)
box.pack_start(fr, False, False, 0)
box.pack_start(fg, False, False, 0)
# flash freq Label & HScale
a=gtk.Adjustment(100, 0, 255, 10, 10)
a.connect('value_changed', flashfreq)
sc = gtk.HScale(a)
sc.set_digits(0)
sc.set_value_pos(gtk.POS_LEFT)
b = gtk.HBox(False, 10)
b.pack_start(gtk.Label('Flash Freq'), False, False)
b.pack_start(sc)
box.pack_start(b, False, False, 0)
# loudspeaker HScale x2
a1=gtk.Adjustment(0, 0, 255, 10, 10)
sc = gtk.HScale(a1)
sc.set_digits(0)
sc.set_value_pos(gtk.POS_LEFT)
b = gtk.HBox(False, 10)
b.pack_start(gtk.Label('LdspkerArg1'), False, False)
b.pack_start(sc)
box.pack_start(b, False, False, 0)
a2=gtk.Adjustment(0, 0, 255, 10, 10)
sc = gtk.HScale(a2)
sc.set_digits(0)
sc.set_value_pos(gtk.POS_LEFT)
b = gtk.HBox(False, 10)
b.pack_start(gtk.Label('LdspkerArg2'), False, False)
b.pack_start(sc)
box.pack_start(b, False, False, 0)
button = gtk.Button('Loudspeaker')
button.connect('clicked', loudspeaker, a1, a2)
box.pack_start(button, False, False, 0)
window.add(box)

window.show_all()
gtk.main()
